package oauth

import (
	"strings"

	"chainmaker.org/chainmaker/smarteditor/logger"
)

var log = logger.Get()

const (
	AuthServerFlag             = "auth_server"
	AuthServerFlagAndStateCode = "auth_server_state_code"

	AuthServerFlagAndStateCodeSplit = "_"
	RedirectFrontPageUrl            = "site"
)

//AuthServer to identify server type
type AuthServer string

const (
	GitLab     AuthServer = "GITLAB"
	ChainMaker AuthServer = "CHAINMAKER"
	SSO        AuthServer = "SSO"
)

func ReadAuthFlagAndState(authFlagAndStateCode string) (string, string, string) {
	authFlagAndStateCodeArr := strings.SplitN(authFlagAndStateCode, AuthServerFlagAndStateCodeSplit, 3)
	if len(authFlagAndStateCodeArr) != 3 {
		return "", "", ""
	}
	return authFlagAndStateCodeArr[0], authFlagAndStateCodeArr[1], authFlagAndStateCodeArr[2]
}

func ConcatAuthFlagAndState(authFlag, stateCode, site string) string {
	return authFlag + AuthServerFlagAndStateCodeSplit + stateCode + AuthServerFlagAndStateCodeSplit + site
}

func GetRedirectUrl(publicServer, oauthServerFlag, stateCode, site string) string {
	return publicServer + "/login/callback?" + AuthServerFlagAndStateCode + "=" + ConcatAuthFlagAndState(oauthServerFlag, stateCode, site)
}
